<?php
namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Kra8\Snowflake\HasSnowflakePrimary;
use Overtrue\EasySms\PhoneNumber;
use SMartins\PassportMultiauth\HasMultiAuthApiTokens;

class User extends Authenticatable
{
    use SoftDeletes;
    use HasSnowflakePrimary,Notifiable,HasMultiAuthApiTokens;

    public $incrementing = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'phone',
        'email',
        'password',
        'wechat_open_id',
        'avatar_url',
        'id_card',
        'role_code',
        'create_group',
        'province',
        'city',
        'area',
        'address',
        'address_detail'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'email',
        'wechat_open_id',
        'email_verified_at'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime'
    ];

    public function role()
    {
        return $this->belongsTo('App\Models\Role', 'role_code', 'code');
    }
    public function getWalletAttribute(){
        return AdminWallet::firstOrCreate(['user_id'=>$this->id]);
    }
    public function bills()
    {
        return $this->hasMany('App\Models\AdminBill', 'user_id');
    }

    public function findForPassport($phone)
    {
        return self::where('phone', $phone)->first();
    }

    public function routeNotificationForEasySms($notification)
    {
        return new PhoneNumber($this->phone, '');
    }
    public function repair_device_groups(){
        return $this->belongsToMany('App\Models\Group','repairer_device_groups','repairer_user_id','device_group_id')->wherePivot('deleted_at',null)->withTimestamps();
    }
}
